package com.lmk.mkcc.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lmk.mkcc.domain.Course;
import com.lmk.mkcc.domain.KillActivity;
import com.lmk.mkcc.domain.KillCourse;
import com.lmk.mkcc.enums.GlobalErrorCode;
import com.lmk.mkcc.feign.CourseService;
import com.lmk.mkcc.mapper.KillCourseMapper;
import com.lmk.mkcc.result.JSONResult;
import com.lmk.mkcc.service.IKillActivityService;
import com.lmk.mkcc.service.IKillCourseService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.lmk.mkcc.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kun
 * @since 2022-10-20
 */
@Service
@Transactional
public class KillCourseServiceImpl extends ServiceImpl<KillCourseMapper, KillCourse> implements IKillCourseService {
    @Autowired
    private CourseService courseService;
    @Autowired
    private IKillActivityService killActivityService;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Override
    public void joinkill(KillCourse killCourse) {
        Long courseId = killCourse.getCourseId();
        // 1.jsr303校验
        // 2.判断课程是否存在
        JSONResult jsonResult = courseService.get(killCourse.getCourseId());
        AssertUtil.isTrue(jsonResult.isSuccess(), GlobalErrorCode.ERROE);
        Object data = jsonResult.getData();
        AssertUtil.isNotNull(data,GlobalErrorCode.DATA_NULL_ERROR);
        // 3.将data参数转化为对象
        Course course = JSON.parseObject(JSON.toJSONString(data), Course.class);
        // 4.判断课程状态，必须说上架
        boolean equals = course.getStatus()==Course.on_status;
        AssertUtil.isTrue(equals,GlobalErrorCode.COURSE_NO_ONLINE_ERROR);
        // 5.判断活动是否是下架状态
        Long activityId = killCourse.getActivityId();
        KillActivity killActivity = killActivityService.selectById(activityId);
        Integer publishStatus = killActivity.getPublishStatus();
        AssertUtil.isNotNull(killActivity, GlobalErrorCode.NO_KILL_ACTIVITY_ERROR);
        // 0表示还未开始活动
        boolean boo = 0 == publishStatus;
        AssertUtil.isTrue(boo, GlobalErrorCode.NO_KILL_ACTIVITY_BIGIN_ERROR);
        // 6.判断课程是否已经在秒杀课程中存在
        KillCourse killcourse = getKillcourseByCourseIdAndActivityId(activityId,courseId);
        AssertUtil.isNull(killcourse,GlobalErrorCode.KILL_COURSE_ERROR);
        // 7.保存为空的数据
        killCourse.setCreateTime(new Date());
        killCourse.setStartTime(killActivity.getStartTime());
        killCourse.setEndTime(killActivity.getEndTime());
        killCourse.setKillLimit(1);
        killCourse.setTimeStr(killActivity.getTimeStr());
        killCourse.setPublishStatus(killActivity.getPublishStatus());
        insert(killCourse);
    }

    @Override
    public List<KillCourse> getkillcourse() {
        // 从redis中获取加入秒杀并且发布的课程
        ArrayList<KillCourse> KillCourse = new ArrayList<>();
        Set<Object> keys = redisTemplate.keys("activity:*");
        for (Object key : keys) {
            List values = redisTemplate.opsForHash().values(key);
            KillCourse.addAll(values);
        }
        return KillCourse;
    }

    @Override
    public KillCourse onlineOne(Long killActivityId, Long killCourseId) {
        Object o = redisTemplate.opsForHash().get("activity:" +killCourseId , killActivityId.toString());
        KillCourse killCourse = (KillCourse)o;
        return killCourse;
    }

    private KillCourse getKillcourseByCourseIdAndActivityId(Long activityId, Long courseId) {
        EntityWrapper<KillCourse> wrapper = new EntityWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("activity_id",activityId);
        KillCourse killCourse = selectOne(wrapper);
        return killCourse;
    }
}
